home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / tetris / tetron / SOURCE / OPENGL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-14  |  39.2 KB  |  1,237 lines

  1. //----------------------------------------------------------------------------------------
  2. //----------------------------------------------------------------------------------------
  3. //
  4. //        Filename        :    opengl.cpp
  5. //        Description        :    Member definitions for OpenGL Class
  6. //        Author            :   Marnich van Rensburg (2002)
  7. //
  8. //----------------------------------------------------------------------------------------
  9. //----------------------------------------------------------------------------------------
  10.  
  11. #include "opengl.h"
  12.  
  13. //----------------------------------------------------------------------------------------
  14. //        Description        :    Class Constructor - Initialize class data members
  15. //----------------------------------------------------------------------------------------
  16. OpenGL :: OpenGL()
  17. {
  18.     hDC        = NULL;    // GDI Device Context
  19.     hRC        = NULL;    // Rendering Context
  20.     hWnd       = NULL;    // Window Handle
  21.     FullScreen = true;    // Sets screen mode to fullscreen by default
  22.  
  23.     //Set Lights Data
  24.     LightAmbient[0] = 1.0f;
  25.     LightAmbient[1] = 1.0f;
  26.     LightAmbient[2] = 1.0f;
  27.     LightAmbient[3] = 1.0f;
  28.  
  29.     LightDiffuse[0] = 1.0f;
  30.     LightDiffuse[1] = 1.0f;
  31.     LightDiffuse[2] = 1.0f;
  32.     LightDiffuse[3] = 1.0f;
  33.  
  34.     LightPosition[0] = 0.0f;
  35.     LightPosition[1] = 0.0f;
  36.     LightPosition[2] = 1.0f;
  37.     LightPosition[3] = 1.0f;
  38.  
  39. }// OpenGL
  40.  
  41.  
  42. //----------------------------------------------------------------------------------------
  43. //        Description        :    Displays an error message box containing the passed message
  44. //----------------------------------------------------------------------------------------
  45. void OpenGL :: ErrorMsg(char *msg)
  46. {
  47.     MessageBox(NULL, msg, "ERROR", MB_OK | MB_ICONEXCLAMATION);
  48.  
  49. }// ErrorMsg
  50.  
  51.  
  52. //----------------------------------------------------------------------------------------
  53. //        Description        :    Initialize OpenGL settings
  54. //----------------------------------------------------------------------------------------
  55. bool OpenGL :: InitGL()
  56. {
  57.     glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  58.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);                // Background Color
  59.     glClearDepth(1.0f);                                    // Depth Buffer Setup
  60.  
  61.     LoadFont();                                            //Init Font
  62.  
  63.     //Perspective
  64.     glDepthFunc(GL_LEQUAL);                                // Type of Depth Testing
  65.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Perspective Calculations
  66.     
  67.     // Load Textures
  68.     if (!LoadGLTextures()) return false;
  69.  
  70.     glEnable(GL_TEXTURE_2D);                            // Enable Texture Mapping
  71.     glDisable(GL_DEPTH_TEST);                            // Turn Depth Testing Off
  72.  
  73.     //Transparency
  74.     glDisable(GL_BLEND);                                    // Turn Blending On
  75.     glColor4f(1.0f, 1.0f, 1.0f, 0.5f);                    // Full Brightness.  50% Alpha
  76.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);                    // Set The Blending Function For Translucency
  77.     
  78.     //Lights
  79.     glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);        // Setup The Ambient Light
  80.     glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);        // Setup The Diffuse Light
  81.     glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);    // Position The Light
  82.     glEnable(GL_LIGHT1);                                // Enable Light One
  83.     glEnable(GL_LIGHTING);
  84.  
  85.     //init stars
  86.     float t_x, t_y;
  87.     for(int n = 0; n < NUMBER_OF_STARS; n++)
  88.     {
  89.         t_x = t_y = 0.0f;
  90.         while(t_x == 0.0f)
  91.             t_x = (float)(rand() % 50) - 25.0f;
  92.  
  93.         while(t_y == 0.0f)
  94.             t_y = (float)(rand() % 50) - 25.0f;
  95.  
  96.         StarField[n].x = t_x;
  97.         StarField[n].y = t_y;
  98.     }//for
  99.  
  100.     return true;
  101.  
  102. }// InitGL
  103.  
  104.  
  105.  
  106.  
  107.  
  108. void OpenGL :: RenderStart()
  109. {
  110.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // Clear Screen And Depth Buffer
  111.  
  112.     glEnable(GL_LIGHTING);
  113.     glEnable(GL_BLEND);
  114.     glEnable(GL_TEXTURE_2D);                            // Enable Texture Mapping
  115.  
  116.     glLoadIdentity();
  117.  
  118. }// RenderStart
  119.  
  120.  
  121.  
  122.  
  123. //----------------------------------------------------------------------------------------
  124. //        Description        :    Dummy function (not implemented)
  125. //----------------------------------------------------------------------------------------
  126.  
  127. void OpenGL :: RenderEnd()
  128. {
  129.     return;
  130.  
  131. }// RenderEnd
  132.  
  133.  
  134.  
  135.  
  136. //----------------------------------------------------------------------------------------
  137. //        Description        :    Renders "TETRON" on the screen
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void OpenGL :: RenderGameTitle(float v_x, float v_y)
  141. {
  142.  
  143.     glEnable(GL_LIGHTING);
  144.  
  145.     glLoadIdentity();
  146.     glTranslatef(v_x, v_y, -10.0f);
  147.  
  148.     glBindTexture(GL_TEXTURE_2D, Texture[11]);
  149.  
  150.     glBegin(GL_QUADS);
  151.         glNormal3f( 0.0f, 0.0f, 1.0f);
  152.         glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f, 0.0f, 0.0f);
  153.         glTexCoord2f(1.0f, 0.0f); glVertex3f( 2.0f, 0.0f, 0.0f);
  154.         glTexCoord2f(1.0f, 1.0f); glVertex3f( 2.0f, 1.0f, 0.0f);
  155.         glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
  156.     glEnd();
  157.  
  158. }// RenderGameTitle
  159.  
  160.  
  161. //----------------------------------------------------------------------------------------
  162. //        Description        :    Types "Game Over" on the screen
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void OpenGL :: RenderGameOver(char Mode[4])
  166. {
  167.     int n = 0;
  168.  
  169.     SetTextBase(32);
  170.     static char Str[] = "GAME OVER";
  171.     static int cn = 0;
  172.     static float B[9] = {0.0f};
  173.  
  174.     if(Mode == "New")
  175.     {    
  176.         cn = 0;
  177.         for(n = 0; n < 9; n++)
  178.             B[n] = 0.0f;
  179.         return;
  180.     }//if
  181.  
  182.  
  183.     if(B[cn] < 1.0f) 
  184.     {
  185.         B[cn] += 0.025f;
  186.     }else{    
  187.         if(cn < 8) 
  188.         {
  189.             cn++;
  190.             if(SoundActive) PlaySound("data/gameover.wav", NULL, SND_ASYNC);// Play sound
  191.         }
  192.     }//if
  193.  
  194.     for(n = 0; n <= cn; n++)
  195.     {
  196.         glColor3f(B[n], B[n], B[n]);
  197.         Print(176 + (n * 32), 240, "%c", Str[n]);
  198.     }//for
  199.  
  200. }//RenderGameOver
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //        Description        :    Shows the next piece
  204. //----------------------------------------------------------------------------------------
  205.  
  206. void OpenGL :: RenderNextPreview(Tetramino& ref_TetNext)
  207. {
  208.     //Draw Tetramino
  209.     glLoadIdentity();
  210.     glTranslatef(-14.0f, 7.5f, -30.0f);
  211.  
  212.     for (char ny = 0; ny < 4; ny++)
  213.     {
  214.         for (char nx = 0; nx < 4; nx++)
  215.         {
  216.             glTranslatef(1.0f, 0.0f, 0.0f);
  217.             if(ref_TetNext.GetMatrix(nx, ny) != 0) 
  218.                 DrawFace(ref_TetNext.Type);            
  219.         }//for
  220.         glTranslatef(-4.0f, -1.0f, 0.0f);
  221.     }//for
  222.  
  223.     //Draw Preview Box
  224.     glLoadIdentity();
  225.     glTranslatef(-2.5f, 0.5f, -5.0f);
  226.  
  227.     glBindTexture(GL_TEXTURE_2D, Texture[9]);
  228.  
  229.     glBegin(GL_QUADS);
  230.         glNormal3f( 0.0f, 0.0f, 1.0f);
  231.         glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f, 0.0f, 0.0f);
  232.         glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 0.0f, 0.0f);
  233.         glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
  234.         glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
  235.     glEnd();
  236.     
  237.     //Draw Preview arrow
  238.     glTranslatef(1.0f, 0.0f, 0.0f);
  239.  
  240.     glBindTexture(GL_TEXTURE_2D, Texture[10]);
  241.  
  242.     glBegin(GL_QUADS);
  243.         glNormal3f( 0.0f, 0.0f, 1.0f);
  244.         glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f,  0.0f, 0.0f);
  245.         glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.25f, 0.0f, 0.0f);
  246.         glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.25f, 1.0f, 0.0f);
  247.         glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f,  1.0f, 0.0f);
  248.     glEnd();
  249.  
  250. }// RenderNextPreview
  251.  
  252.  
  253.  
  254.  
  255. //----------------------------------------------------------------------------------------
  256. //        Description        :    Shows arrows depending on the rotation direction of the container
  257. //----------------------------------------------------------------------------------------
  258.  
  259. void OpenGL :: RenderRotationArrows(unsigned char v_UserAction)
  260. {
  261.     static float B[2] = {0.0f};                // Store the brightness level of arrows
  262.     static float RA = 0.0f;                    // Store current rotation angle of arrows
  263.  
  264.     switch(v_UserAction)                    // Identify which user action should be shown
  265.     {
  266.         case 39: {if(B[0] < 1.0f) B[0] += 0.5f; break;}    // Right Arrow
  267.         case 37: {if(B[1] < 1.0f) B[1] += 0.5f; break;} // Left Arrow
  268.     }//switch
  269.  
  270.     // Draw the left and right arrows
  271.     glDisable(GL_LIGHTING);
  272.     glEnable(GL_BLEND);
  273.     glDisable(GL_TEXTURE_2D);
  274.  
  275.     // Draw Right Arrow
  276.     glLoadIdentity();
  277.     glTranslatef(8.0f, 0.0f, -30.0f);
  278.     glRotatef(RA, 1.0f, 0.0f, 0.0f);
  279.  
  280.     glBegin(GL_TRIANGLES);
  281.         glColor3f(B[0], B[0], B[0]);glVertex3f( 2.0f, 0.0f, 0.0f);        
  282.         glColor3f(0.0f, 0.0f, 0.0f);glVertex3f(-1.0f, 1.0f, 0.0f);
  283.         glColor3f(B[0], B[0], B[0]);glVertex3f( 0.0f, 0.0f, 0.0f);
  284.  
  285.         glColor3f(B[0], B[0], B[0]);glVertex3f( 2.0f, 0.0f, 0.0f);
  286.         glColor3f(0.0f, 0.0f, 0.0f);glVertex3f(-1.0f,-1.0f, 0.0f);
  287.         glColor3f(B[0], B[0], B[0]);glVertex3f( 0.0f, 0.0f, 0.0f);
  288.     glEnd();
  289.  
  290.     // Draw Left Arrow
  291.     glLoadIdentity();
  292.     glTranslatef(-8.0f, 0.0f, -30.0f);
  293.     glRotatef(RA * -1.0f, 1.0f, 0.0f, 0.0f);
  294.  
  295.     glBegin(GL_TRIANGLES);
  296.         glColor3f(B[1], B[1], B[1]);glVertex3f(-2.0f, 0.0f, 0.0f);
  297.         glColor3f(0.0f, 0.0f, 0.0f);glVertex3f( 1.0f, 1.0f, 0.0f);
  298.         glColor3f(B[1], B[1], B[1]);glVertex3f( 0.0f, 0.0f, 0.0f);
  299.  
  300.         glColor3f(B[1], B[1], B[1]);glVertex3f(-2.0f, 0.0f, 0.0f);
  301.         glColor3f(0.0f, 0.0f, 0.0f);glVertex3f( 1.0f,-1.0f, 0.0f);    
  302.         glColor3f(B[1], B[1], B[1]);glVertex3f( 0.0f, 0.0f, 0.0f);
  303.     glEnd();    
  304.  
  305.     glEnable(GL_TEXTURE_2D);
  306.     glEnable(GL_LIGHTING);
  307.     glEnable(GL_BLEND);
  308.  
  309.     RA += 2.0f;                        // Inc. current angle by 5
  310.  
  311.     // Perform slow decay of arrow B
  312.     if(B[0] > 0.0f) B[0] -= 0.01f;    // Decrease Brightness of left arrow
  313.     if(B[1] > 0.0f) B[1] -= 0.01f;    // Decrease Brightness of right arrow
  314.  
  315. }// RenderRotationArrows
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322. //----------------------------------------------------------------------------------------
  323. //        Description        :    Draws a textured quad based on type value
  324. //----------------------------------------------------------------------------------------
  325.  
  326. void OpenGL :: DrawFace(char v_Type)
  327. {
  328.     glBindTexture(GL_TEXTURE_2D, Texture[v_Type]);
  329.  
  330.     glBegin(GL_QUADS);
  331.         // Front Face
  332.         glNormal3f( 0.0f, 0.0f, 1.0f);
  333.         glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f,-0.5f, 0.0f);
  334.         glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f,-0.5f, 0.0f);
  335.         glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, 0.5f, 0.0f);
  336.         glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.0f);
  337.     glEnd();
  338.  
  339. }// DrawFace
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346. //----------------------------------------------------------------------------------------
  347. //        Description        :    Draws the container object
  348. //----------------------------------------------------------------------------------------
  349.  
  350. void OpenGL :: RenderContainer(Container& r_Con)
  351. {
  352.     glColor3f(1.0f, 1.0f, 1.0f);
  353.     glEnable(GL_BLEND);
  354.     glDisable(GL_DEPTH_TEST);
  355.     glEnable(GL_LIGHTING);
  356.  
  357.     glLoadIdentity();                                    // Reset The View
  358.     glTranslatef( r_Con.x, r_Con.y + (float)(CON_HEIGHT / 2), r_Con.z); //Set position
  359.     glRotatef(r_Con.GetRotation() - 20.0f, 0.0f, 1.0f, 0.0f);    //y rotate container
  360.  
  361.     // Draw Container Cells Top to Bottom, Left to Right
  362.     for (char cy = 0; cy != CON_HEIGHT; cy++)            //loop through the vertical cells
  363.     {
  364.         glTranslatef( 0.0f, -1.0f, 0.0f);                //Move 1 row down
  365.         for (char cx = 0; cx != CON_WIDTH; cx++)        //loop through the horizontal cells
  366.         {
  367.             glRotatef(10.0f, 0.0f, 1.0f, 0.0f);            //Rotate 10 degrees to right        
  368.             glTranslatef( 0.0f, 0.0f, CON_RADIUS);        //Move out to container surface
  369.             DrawFace(r_Con.GetMatrix(cx, cy));            //Draw the cell face
  370.             glTranslatef( 0.0f, 0.0f, -CON_RADIUS);        //Move back to center
  371.         }//for
  372.     }//for
  373.  
  374. }// RenderContainer
  375.  
  376.  
  377.  
  378.  
  379. //----------------------------------------------------------------------------------------
  380. //        Description        :    Draws the start screen
  381. //----------------------------------------------------------------------------------------
  382.  
  383. void OpenGL :: RenderStartScreen(Top10& ref_HS)
  384. {
  385.     RenderGameTitle(-1.0f, 2.0f);
  386.  
  387.     int n = 0;
  388.     static char Str[] = "Press SPACE to start#coded by Marnich van Rensburg";
  389.     static int cn = 0;
  390.     static int letter_n = 0;
  391.     static int row, col = 0;
  392.     static Timer TextTimer;
  393.     static int str_len = sizeof(Str) - 1;
  394.     static float B[100] = {0.5f};
  395.  
  396.     glColor3f(1.0f, 0.8f, 0.0f);
  397.     //Highscore Table Col Headings
  398.     Print(230, 210, "* TOP 10 SCORES *");
  399.     glColor3f(0.7f, 0.7f, 0.3f);
  400.     Print(90, 230, "No");
  401.     Print(130, 230, "Name");
  402.     Print(370, 230, "Lines");
  403.     Print(430, 230, "Level");
  404.     Print(490, 230, "Score");
  405.  
  406.     //display table entries
  407.     int tl_row = 0;
  408.     float lcolor = 1.0f;
  409.     for(n = 0; n < 10; n++)
  410.     {
  411.         tl_row = 250 + ((n + 1) * 15);
  412.         lcolor = 1.0f - ((float)(n+1) / 20.0f);
  413.         glColor3f(0.0f, 0.0f, lcolor);
  414.         if(n<9)
  415.             Print(105, tl_row, "%i", n + 1);    
  416.         else
  417.             Print(95, tl_row, "%i", n + 1);    
  418.  
  419.         Print(130, tl_row, "%s", ref_HS.List[n].Name);    
  420.         Print(370, tl_row, "%u", ref_HS.List[n].Lines);    
  421.         Print(430, tl_row, "%u", ref_HS.List[n].Level);    
  422.         Print(490, tl_row, "%u", ref_HS.List[n].Score);    
  423.     }//for
  424.  
  425.     if(TextTimer.CheckFreq(90) && letter_n < str_len) 
  426.     {
  427.         cn++;
  428.         B[cn] = 0.5f;
  429.         letter_n++;
  430.         if(Str[letter_n] != '#') 
  431.             if(SoundActive) PlaySound("data/start.wav", NULL, SND_ASYNC);// Play sound
  432.     }
  433.  
  434.     SetTextBase(32);
  435.     row = 17;
  436.     col = 21;
  437.     for(n = 0; n <= letter_n; n++)
  438.     {
  439.         if(B[n] < 1.0f) B[n] += 0.001f;
  440.         glColor3f(B[n], B[n], B[n]);
  441.         if(Str[n] == '#')
  442.         {
  443.             row = 47;
  444.             col = 33;
  445.         }else{
  446.             col++;
  447.             Print((col * 10), (row * 10), "%c", Str[n]);
  448.         }
  449.     }//for
  450.  
  451. }//end RenderStartScreen
  452.  
  453.  
  454.  
  455. void OpenGL :: DebugVal(float v_Val)
  456. {
  457.     SetTextBase(32);
  458.     glColor3f(1.0f, 1.0f, 1.0f);
  459.     Print(40, 40, "Val = %f", v_Val);
  460. }
  461.  
  462. //----------------------------------------------------------------------------------------
  463. //        Description        :    Draws the container object
  464. //----------------------------------------------------------------------------------------
  465.  
  466. void OpenGL :: RenderHighScoreScreen(char v_Name[25], char v_CursorPos, unsigned int v_Score)
  467. {
  468.     static float CursorCol = 0.0f;
  469.     CursorCol += 0.025f;
  470.     if(CursorCol >= 1.0f) CursorCol = 0.0f;
  471.  
  472.     SetTextBase(32);
  473.     glColor3f(1.0f, 1.0f, 1.0f);
  474.     Print(210, 200, " * New Top 10 Score *");
  475.     Print(210, 230, "         %i", v_Score);
  476.     Print(210, 260, "Please enter your name:");
  477.  
  478.     for(int n = 0; n < 23; n++)
  479.         Print(210 + (n * 10), 280, "%c", v_Name[n]);
  480.     
  481.     glColor3f(CursorCol, CursorCol, CursorCol);
  482.     Print(210 + (v_CursorPos * 10), 285, "_");
  483.  
  484. }//end RenderHighScoreScreen
  485.  
  486.  
  487.  
  488. //----------------------------------------------------------------------------------------
  489. //        Description        :    Draws a graph line 
  490. //----------------------------------------------------------------------------------------
  491.  
  492. void OpenGL :: DrawGraphLine(float v_x, float v_y, float v_z, float v_Length)
  493. {
  494.     glDisable(GL_LIGHTING);
  495.     glDisable(GL_BLEND);
  496.     glDisable(GL_DEPTH_TEST);
  497.     glDisable(GL_TEXTURE_2D);
  498.     
  499.     glLoadIdentity();
  500.     glTranslatef(v_x, v_y, v_z);
  501.     
  502.     v_Length /= 12.5f;
  503.  
  504.     glBegin(GL_QUADS);
  505.         glColor3f(0.2f, 0.2f, 0.2f);
  506.         glVertex3f(0.0f, -0.2f, 0.0f);
  507.         glColor3f(0.7f, 0.7f, 0.7f);
  508.         glVertex3f(v_Length, -0.2f, 0.0f);
  509.         glColor3f(0.9f, 0.7f, 0.7f);
  510.         glVertex3f(v_Length,  0.2f, 0.0f);
  511.         glColor3f(0.3f, 0.6f, 0.3f);
  512.         glVertex3f(0.0f, 0.2f, 0.0f);
  513.     glEnd();
  514.  
  515.     glColor3f(1.0f, 1.0f, 1.0f);
  516.     glEnable(GL_TEXTURE_2D);
  517.     glEnable(GL_LIGHTING);
  518.     glEnable(GL_BLEND);
  519.  
  520.     glLoadIdentity();
  521.     glTranslatef(v_x, v_y, v_z);
  522.     
  523.     glBindTexture(GL_TEXTURE_2D, Texture[13]);
  524.  
  525.     glBegin(GL_QUADS);
  526.         glNormal3f( 0.0f, 0.0f, 1.0f);
  527.         glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f,-0.4f, 0.0f);
  528.         glTexCoord2f(1.0f, 0.0f); glVertex3f(8.0f,-0.4f, 0.0f);
  529.         glTexCoord2f(1.0f, 1.0f); glVertex3f(8.0f, 0.4f, 0.0f);
  530.         glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.4f, 0.0f);
  531.     glEnd();
  532.  
  533.  
  534. }//end DrawGraphLine
  535.  
  536.  
  537.  
  538. //----------------------------------------------------------------------------------------
  539. //        Description        :    Renders game stats on screen (score etc.)
  540. //----------------------------------------------------------------------------------------
  541.  
  542. void OpenGL :: RenderGameStats(GameStats& ref_Stats)
  543. {
  544.     static unsigned int VisScore = 0;    // The score currently shown, not actual ness. game score
  545.     static float VisAvg[7] = {0.0f};    // The avg currently shown
  546.  
  547.     //Ajust dif. between actual game score and the score shown
  548.     if (ref_Stats.Score > VisScore) VisScore += (int)((ref_Stats.Score - VisScore) / 10);
  549.     if ((ref_Stats.Score < VisScore) && (VisScore > 0)) VisScore = ref_Stats.Score;
  550.  
  551.     glColor3f(1.0f, 1.0f, 1.0f);
  552.     SetTextBase(32);
  553.     Print(430,100,"SCORE [ %2i ]",(int)VisScore);
  554.     Print(430,130,"LINES [ %2i ]",(int)ref_Stats.Lines);
  555.     Print(430,160,"LEVEL [ %2i ]",(int)ref_Stats.Level);
  556.  
  557.     float TetAvg = 0.0f;
  558.  
  559.     glEnable(GL_BLEND);
  560.     glDisable(GL_DEPTH_TEST);
  561.     glEnable(GL_LIGHTING);
  562.  
  563.     //Draw tetramino stats
  564.     for(int type = 0; type != 7; type++)
  565.     {
  566.         glLoadIdentity();
  567.         glTranslatef(7.0f, -2.0f - (float)(type * 1.2f), -32.0f);
  568.         DrawFace(type + 1);
  569.         //prevent division by zero
  570.         if(ref_Stats.TetTotal)
  571.             TetAvg = ((float)ref_Stats.TetTypeCount[type] / (float)ref_Stats.TetTotal) * 100.0f;    // calc average
  572.         else
  573.             TetAvg = 0.0f;
  574.         
  575.         //Print(470,282 + (type * 22)," [ %2i%% ]",(int)TetAvg);
  576.  
  577.         if(TetAvg > VisAvg[type])
  578.             VisAvg[type] += 0.2f;
  579.  
  580.         if(TetAvg < VisAvg[type])
  581.             VisAvg[type] -= 0.2f;
  582.  
  583.         DrawGraphLine(8.5f, -2.0f - (float)(type * 1.2f), -32.0f, VisAvg[type]);
  584.  
  585.     }//for
  586.  
  587. }// RenderGameStats
  588.  
  589.  
  590.  
  591. //----------------------------------------------------------------------------------------
  592. //        Description        :    Displays a rotating arrow for the lenght of the drop
  593. //----------------------------------------------------------------------------------------
  594.  
  595. void OpenGL :: RenderDropArrow(char v_Start, char v_End, bool v_DropState)
  596. {
  597.     static float B = 0.0f;                // Store the brightness level of arrow
  598.     static float Angle = 0.0f;            // Store current angle of arrow
  599.     static float Top = 0.0f;            // Stores Top position of arrow
  600.     static float Length = 0.0f;            // Stores Length of arrow
  601.  
  602.     if(v_DropState)
  603.     { 
  604.         Top = 10.0f - (float)v_Start;    // Calc Top of arrow
  605.         Length = ((float)v_End - (float)v_Start);    // Calc Bottom of arrow
  606.         B = 1.0f;                        // Set to full brightness
  607.     }else{
  608.         if(B > 0.0f) B -= 0.01f;        // Slow Brightness decay
  609.         
  610.         if(Length > 0.0f)
  611.         { 
  612.             Top -= 0.5f;                             
  613.             Length -= 0.5f;                // Arrow recoil
  614.         }//if
  615.     }//if - else
  616.     
  617.     Angle += 10.0f;
  618.  
  619.     //Draw arrow
  620.     glDisable(GL_LIGHTING);
  621.     glEnable(GL_BLEND);
  622.     glDisable(GL_TEXTURE_2D);            // Enable Texture Mapping
  623.     
  624.     glLoadIdentity();
  625.     glTranslatef(0.0f, Top, -32.0f);
  626.     glRotatef(Angle, 0.0f, 1.0f, 0.0f);
  627.  
  628.     glBegin(GL_TRIANGLES);
  629.         glColor3f(0.0f, 0.0f, 0.0f);glVertex3f(-2.0f, 0.0f, 0.0f);
  630.         glColor3f(B, B, B);glVertex3f( 0.0f, 0.0f - Length,  0.0f);
  631.         glColor3f(0.0f, 0.0f, 0.0f);glVertex3f( 2.0f, 0.0f, 0.0f);
  632.     glEnd();
  633.  
  634.     glEnable(GL_TEXTURE_2D);            // Enable Texture Mapping
  635.     glEnable(GL_LIGHTING);
  636.  
  637. }// RenderDropArrow
  638.  
  639.  
  640.  
  641. //----------------------------------------------------------------------------------------
  642. //        Description        :    Draws a star at x,y,z
  643. //----------------------------------------------------------------------------------------
  644.  
  645. void OpenGL :: DrawStar(float v_x, float v_y, float v_z)
  646. {
  647.     static float Clr[3] = {1.0f, 0.0f, 1.0f};        // 3 color values
  648.     static float ClrInc[3] = {0.000001f, 0.000002f, 0.000003f};    // increment speed of each color
  649.     float zClr = 0.0f;
  650.  
  651.     // Color cycle
  652.     for(int n = 0; n < 3; n++)
  653.     {
  654.         Clr[n] = Clr[n] + ClrInc[n];
  655.         if(Clr[n] > 1.0f)
  656.         {
  657.             Clr[n] = 1.0f;
  658.             ClrInc[n] = -ClrInc[n];
  659.         }//if
  660.  
  661.         if(Clr[n] < 0.5f)
  662.         {
  663.             Clr[n] = 0.5f;
  664.             ClrInc[n] = -ClrInc[n];
  665.         }//if
  666.     }//for
  667.  
  668.     //glColor4ub((char)(v_z * 2.0f), 0, 0, 255);
  669.     zClr = (-v_z / 10.0f);
  670.     glColor3f(Clr[0] / zClr, Clr[1] / zClr, Clr[2] / zClr);
  671.  
  672.     glTranslatef(v_x, v_y, v_z);
  673.  
  674.     glEnable(GL_BLEND);
  675.     glDisable(GL_LIGHTING);
  676.     glDisable(GL_DEPTH_TEST);            // Enable Texture Mapping
  677.  
  678.     glBindTexture(GL_TEXTURE_2D, Texture[12]);
  679.     
  680.     glBegin(GL_QUADS);
  681.         //glNormal3f( 0.0f, 0.0f, 1.0f);
  682.         glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f,-0.5f, 0.0f);
  683.         glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f,-0.5f, 0.0f);
  684.         glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, 0.5f, 0.0f);
  685.         glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.0f);
  686.     glEnd();
  687.  
  688. }//end DrawStar
  689.  
  690.  
  691. //----------------------------------------------------------------------------------------
  692. //        Description        :    Displays a moving starfield
  693. //----------------------------------------------------------------------------------------
  694.  
  695. void OpenGL :: RenderStarField()
  696. {
  697.     if(!StarFieldActive)
  698.         return;
  699.  
  700.     static float rotz = 0.0f;
  701.     static int current_z = 0;
  702.     int n = 0;
  703.  
  704.     if(SFTmr.CheckFreq(30.0f))
  705.     {
  706.         current_z++;
  707.         rotz += 0.2f;
  708.     }//if
  709.  
  710.     if(current_z >= NUMBER_OF_STARS)
  711.         current_z = 0;
  712.  
  713.     float z = 0.0f;
  714.     for(n = current_z; n < NUMBER_OF_STARS; n++)
  715.     {
  716.         z -= 0.5f;
  717.         glLoadIdentity();
  718.         glRotatef(rotz, 0.0f, 0.0f, 1.0f);
  719.         DrawStar(StarField[n].x, StarField[n].y, z);
  720.     }//for
  721.  
  722.     for(n = 0; n < current_z; n++)
  723.     {
  724.         z -= 0.5f;
  725.         glLoadIdentity();
  726.         glRotatef(rotz, 0.0f, 0.0f, 1.0f);
  727.         DrawStar(StarField[n].x, StarField[n].y, z);
  728.     }//for
  729.  
  730. }//end RenderStarField
  731.  
  732.  
  733. //----------------------------------------------------------------------------------------
  734. //        Description        :    Sets fullscreen status (true/false)
  735. //----------------------------------------------------------------------------------------
  736. void OpenGL :: SetFullScreen(bool v_FullScreen)
  737. {
  738.    FullScreen = v_FullScreen;
  739.  
  740. }// SetFullScreen
  741.  
  742.  
  743. //----------------------------------------------------------------------------------------
  744. //        Description        :    Returns fullscreen status (true/false)
  745. //----------------------------------------------------------------------------------------
  746. bool OpenGL :: GetFullScreen() const
  747. {
  748.    return FullScreen;
  749.  
  750. }// GetFullScreen
  751.  
  752.  
  753. //----------------------------------------------------------------------------------------
  754. //        Description        :    Returns GDI Device Context
  755. //----------------------------------------------------------------------------------------
  756. HDC OpenGL :: GethDC() const
  757. {
  758.    return hDC;
  759.  
  760. }// GethDC
  761.  
  762.  
  763. //----------------------------------------------------------------------------------------
  764. //        Description        :    Returns Window Handle
  765. //----------------------------------------------------------------------------------------
  766. HWND OpenGL :: GethWnd() const
  767. {
  768.    return hWnd;
  769.  
  770. }// GethWnd
  771.  
  772.  
  773.  
  774. //----------------------------------------------------------------------------------------
  775. //        Description        :    Resize and Initialize the GL Window
  776. //----------------------------------------------------------------------------------------
  777. GLvoid OpenGL :: ReSizeGLScene(GLsizei width, GLsizei height)
  778. {
  779.     if(!height)                            // Prevent A Divide By Zero By
  780.         height = 1;                        // Making Height Equal One
  781.  
  782.     glViewport(0, 0, width, height);    // Reset The Current Viewport
  783.  
  784.     glMatrixMode(GL_PROJECTION);        // Select The Projection Matrix
  785.     glLoadIdentity();                    // Reset The Projection Matrix
  786.  
  787.     // Calculate The Aspect Ratio Of The Window
  788.     gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  789.  
  790.     glMatrixMode(GL_MODELVIEW);            // Select The Modelview Matrix
  791.     glLoadIdentity();                    // Reset The Modelview Matrix
  792.  
  793. }// ReSizeGLScene
  794.  
  795.  
  796. //----------------------------------------------------------------------------------------
  797. //        Description        :    Properly kill the window
  798. //----------------------------------------------------------------------------------------
  799. GLvoid OpenGL :: KillGLWindow()
  800. {
  801.  
  802.     ShowCursor(true);                        // Show Mouse Pointer
  803.  
  804.     if (FullScreen)                                // Are We In Fullscreen Mode?
  805.     {
  806.         ChangeDisplaySettings(NULL, 0);            // If So Switch Back To The Desktop
  807.     }//if
  808.  
  809.     if (hRC)                                    // Do We Have A Rendering Context?
  810.     {
  811.         if (!wglMakeCurrent(NULL, NULL))        // Are We Able To Release The DC And RC Contexts?
  812.             ErrorMsg("Release of DC and RC failed.");
  813.  
  814.         if (!wglDeleteContext(hRC))                // Are We Able To Delete The RC?
  815.             ErrorMsg("Release Rendering Context failed.");
  816.  
  817.         hRC = NULL;                                // Set RC To NULL
  818.     }//if
  819.  
  820.     if (hDC && !ReleaseDC(hWnd, hDC))            // Are We Able To Release The DC
  821.     {
  822.         ErrorMsg("Release Device Context failed.");
  823.         hDC = NULL;                                // Set DC To NULL
  824.     }//if
  825.  
  826.     if (hWnd && !DestroyWindow(hWnd))            // Are We Able To Destroy The Window?
  827.     {
  828.         ErrorMsg("Could not release hWnd.");
  829.         hWnd = NULL;                                // Set hWnd To NULL
  830.     }//if
  831.  
  832.     if (!UnregisterClass("OpenGL", hInstance))    // Are We Able To Unregister Class
  833.     {
  834.         ErrorMsg("Could Not Unregister Class.");
  835.         hInstance=NULL;                            // Set hInstance To NULL
  836.     }//if
  837.  
  838. }// KillGLWindow
  839.  
  840.  
  841.  
  842. //----------------------------------------------------------------------------------------
  843. //        Description        :    Creates the OpenGL Window
  844. //
  845. //        Parameters        :
  846. //            title            - Title to appear at the top of the window.
  847. //            width            - Width of the GL Window or fullscreen mode.
  848. //            height            - Height of the GL Window or fullscreen mode.
  849. //            bits            - Number of bits to use for color (8/16/24/32).
  850. //            fullscreenflag    - Pass (true) for Fullscreen Mode or (false) for Windowed Mode.
  851. //----------------------------------------------------------------------------------------
  852.  
  853. bool OpenGL :: CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
  854. {
  855.     GLuint   PixelFormat;        // Holds the results after searching for a match
  856.     WNDCLASS wc;                // Windows Class Structure
  857.     DWORD    dwExStyle;            // Window Extended Style
  858.     DWORD    dwStyle;            // Window Style
  859.     RECT     WindowRect;        // Grabs Rectangle Upper Left / Lower Right Values
  860.  
  861.     WindowRect.left   = (long)0;        // Set Left Value To 0
  862.     WindowRect.right  = (long)width;    // Set Right Value To Requested Width
  863.     WindowRect.top    = (long)0;        // Set Top Value To 0
  864.     WindowRect.bottom = (long)height;    // Set Bottom Value To Requested Height
  865.  
  866.     FullScreen = fullscreenflag;        // Set The Global Fullscreen Flag
  867.  
  868.     hInstance        = GetModuleHandle(NULL);                // Grab an instance for our window
  869.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;    // Redraw on size, and own dc for window.
  870.     wc.lpfnWndProc   = (WNDPROC) WndProc;                    // WndProc Handles Messages
  871.     wc.cbClsExtra    = 0;                                    // No extra window data
  872.     wc.cbWndExtra    = 0;                                    // No extra window data
  873.     wc.hInstance     = hInstance;                            // Set the instance
  874.     wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);            // Load the default icon
  875.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);            // Load the arrow pointer
  876.     wc.hbrBackground = NULL;                                // No background required for GL
  877.     wc.lpszMenuName  = NULL;                                // We don't want a menu
  878.     wc.lpszClassName = "OpenGL";                            // Set the class name
  879.  
  880.     if (!RegisterClass(&wc))            // Attempt to register the window class
  881.     {
  882.         ErrorMsg("Failed to register the Window Class.");
  883.         return false;
  884.     }//if
  885.  
  886.     if (FullScreen)                                                    // Attempt Fullscreen Mode?
  887.     {
  888.         DEVMODE dmScreenSettings;                                    // Device Mode
  889.         memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));        // Makes sure memory's cleared
  890.         dmScreenSettings.dmSize       = sizeof(dmScreenSettings);    // Size of the devmode structure
  891.         dmScreenSettings.dmPelsWidth  = width;                        // Selected Screen Width
  892.         dmScreenSettings.dmPelsHeight = height;                        // Selected Screen Height
  893.         dmScreenSettings.dmBitsPerPel = bits;                        // Selected Bits Per Pixel
  894.         dmScreenSettings.dmFields     = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  895.  
  896.         // Try to set selected mode and get results.  NOTE: CDS_FULLSCREEN Gets rid of start bar.
  897.         if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  898.         {
  899.             // If the mode fails, offer two options.  Quit or use windowed mode.
  900.             if (MessageBox(NULL, "The requested fullscreen mode is not supported by\nyour video card. Use windowed mode instead?", "OpenGL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
  901.             {
  902.                 FullScreen = false;        // Windowed Mode Selected.  Fullscreen = false
  903.             }
  904.             else
  905.             {
  906.                 MessageBox(NULL, "Program will now close.", "ERROR", MB_OK | MB_ICONSTOP);
  907.                 return false;
  908.             }//if - else
  909.         }//if
  910.     }//if
  911.  
  912.     if (FullScreen)                        // Are we still in fullscreen mode?
  913.     {
  914.         dwExStyle=WS_EX_APPWINDOW;        // Window Extended Style
  915.         dwStyle=WS_POPUP;                // Windows Style
  916.         ShowCursor(false);                // Hide Mouse Pointer
  917.     }
  918.     else
  919.     {
  920.         dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;        // Window Extended Style
  921.         dwStyle=WS_OVERLAPPEDWINDOW;                        // Windows Style
  922.     }//if - else
  923.  
  924.     AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle);        // Adjust window to true requested size
  925.  
  926.     // Create The Window
  927.     if (!(hWnd=CreateWindowEx(   
  928.                     dwExStyle,                            // Extended style for the window
  929.                     "OpenGL",                            // Class Name
  930.                     title,                                // Window Title
  931.                     dwStyle |                            // Defined Window Style
  932.                     WS_CLIPSIBLINGS |                    // Required Window Style
  933.                     WS_CLIPCHILDREN,                    // Required Window Style
  934.                     0, 0,                                // Window Position
  935.                     WindowRect.right-WindowRect.left,    // Calculate Window Width
  936.                     WindowRect.bottom-WindowRect.top,    // Calculate Window Height
  937.                     NULL,                                // No Parent Window
  938.                     NULL,                                // No Menu
  939.                     hInstance,                            // Instance
  940.                     NULL)))                                // Don't pass anything to WM_CREATE
  941.     {
  942.         KillGLWindow();
  943.         ErrorMsg("Window Creation Error.");
  944.         return false;
  945.     }//if
  946.  
  947.  
  948.     static PIXELFORMATDESCRIPTOR pfd=            // pfd Tells windows how we want things to be
  949.     {
  950.         sizeof(PIXELFORMATDESCRIPTOR),            // Size of this Pixel Format Descriptor
  951.         1,                                        // Version Number
  952.         PFD_DRAW_TO_WINDOW |                    // Format Must Support Window
  953.         PFD_SUPPORT_OPENGL |                    // Format Must Support OpenGL
  954.         PFD_DOUBLEBUFFER,                        // Must Support Double Buffering
  955.         PFD_TYPE_RGBA,                            // Request An RGBA Format
  956.         bits,                                    // Select Our Color Depth
  957.         0, 0, 0, 0, 0, 0,                        // Color Bits Ignored
  958.         0,                                        // No Alpha Buffer
  959.         0,                                        // Shift Bit Ignored
  960.         0,                                        // No Accumulation Buffer
  961.         0, 0, 0, 0,                                // Accumulation Bits Ignored
  962.         16,                                        // 16Bit Z-Buffer (Depth Buffer)  
  963.         0,                                        // No Stencil Buffer
  964.         0,                                        // No Auxiliary Buffer
  965.         PFD_MAIN_PLANE,                            // Main Drawing Layer
  966.         0,                                        // Reserved
  967.         0, 0, 0                                    // Layer Masks Ignored
  968.     };//End static
  969.  
  970.     if (!(hDC = GetDC(hWnd)))                    // Did we get a device context?
  971.     {
  972.         KillGLWindow();                            // Reset The Display
  973.         ErrorMsg("Can't create a GL device context.");
  974.         return false;
  975.     }//if
  976.  
  977.     if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))    // Did windows find a matching pixel format?
  978.     {
  979.         KillGLWindow();                            // Reset The Display
  980.         ErrorMsg("Can't find a suitable pixel format.");
  981.         return false;
  982.     }//if
  983.  
  984.     if(!SetPixelFormat(hDC, PixelFormat, &pfd))    // Are we able to set the pixel format?
  985.     {
  986.         KillGLWindow();                            // Reset The Display
  987.         ErrorMsg("Can't set the pixel format.");
  988.         return false;
  989.     }//if
  990.  
  991.     if (!(hRC = wglCreateContext(hDC)))            // Are We Able To Get A Rendering Context?
  992.     {
  993.         KillGLWindow();                            // Reset The Display
  994.         ErrorMsg("Can't create a GL Rendering Context.");
  995.         return false;
  996.     }//if
  997.  
  998.     if(!wglMakeCurrent(hDC,hRC))                // Try To Activate The Rendering Context
  999.     {
  1000.         KillGLWindow();                            // Reset The Display
  1001.         ErrorMsg("Can't activate the GL Rendering Context.");
  1002.         return false;
  1003.     }//if
  1004.  
  1005.     ShowWindow(hWnd,SW_SHOW);                    // Show The Window
  1006.     SetForegroundWindow(hWnd);                    // Slightly Higher Priority
  1007.     SetFocus(hWnd);                                // Sets Keyboard Focus To The Window
  1008.     ReSizeGLScene(width, height);                // Set Up Our Perspective GL Screen
  1009.  
  1010.     if (!InitGL())                                // Initialize Our Newly Created GL Window
  1011.     {
  1012.         KillGLWindow();                            // Reset The Display
  1013.         ErrorMsg("Initialization Failed.");
  1014.         return false;
  1015.     }//if
  1016.  
  1017.     return true;    //No errors occured return true
  1018.     
  1019. }// CreateGLWindow
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025. //----------------------------------------------------------------------------------------
  1026. //        Description        :    Loads a bitmap file and returns a pointer to the bitmap data
  1027. //
  1028. //        Parameters        :
  1029. //            Filename        - name and path of a bitmap file eg. (path/file.bmp)
  1030. //----------------------------------------------------------------------------------------
  1031.  
  1032. AUX_RGBImageRec* OpenGL :: LoadBMP(char *Filename)
  1033. {
  1034.     FILE *File = NULL;                                // File Handle
  1035.  
  1036.     if (!Filename)                                    // Make Sure A Filename Was Given
  1037.         return NULL;                                // If Not Return NULL
  1038.  
  1039.     File = fopen(Filename, "r");                    // Check To See If The File Exists
  1040.  
  1041.     if (File)                                        // Does The File Exist?
  1042.     {
  1043.         fclose(File);                                // Close The Handle
  1044.         return auxDIBImageLoad(Filename);            // Load The Bitmap And Return A Pointer
  1045.     }//if
  1046.  
  1047.     return NULL;                                    // If Load Failed Return NULL
  1048.  
  1049. }// LoadBMP
  1050.  
  1051.  
  1052.  
  1053.  
  1054. //----------------------------------------------------------------------------------------
  1055. //        Description        :    Loads all the required OpenGL textures into memory
  1056. //----------------------------------------------------------------------------------------
  1057.  
  1058. bool OpenGL :: LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
  1059. {      
  1060.         bool Status = FALSE;                                    // Status Indicator
  1061.         int loop1 = 0;
  1062.  
  1063.         AUX_RGBImageRec *TextureImage[NUMBER_OF_TEXTURES];                    // Create Storage Space For The Textures
  1064.         memset(TextureImage,0,sizeof(void *)*NUMBER_OF_TEXTURES);            // Set The Pointer To NULL
  1065.  
  1066.         if ((TextureImage[0]=LoadBMP("data/0.bmp")) &&
  1067.             (TextureImage[1]=LoadBMP("data/1.bmp")) &&
  1068.             (TextureImage[2]=LoadBMP("data/2.bmp")) &&
  1069.             (TextureImage[3]=LoadBMP("data/3.bmp")) &&
  1070.             (TextureImage[4]=LoadBMP("data/4.bmp")) &&
  1071.             (TextureImage[5]=LoadBMP("data/5.bmp")) &&
  1072.             (TextureImage[6]=LoadBMP("data/6.bmp")) &&
  1073.               (TextureImage[7]=LoadBMP("data/7.bmp")) &&
  1074.               (TextureImage[8]=LoadBMP("data/8.bmp")) &&
  1075.               (TextureImage[9]=LoadBMP("data/9.bmp")) &&
  1076.               (TextureImage[10]=LoadBMP("data/10.bmp")) &&
  1077.               (TextureImage[11]=LoadBMP("data/11.bmp")) &&
  1078.               (TextureImage[12]=LoadBMP("data/12.bmp")) &&
  1079.             (TextureImage[13]=LoadBMP("data/13.bmp"))) 
  1080.         {
  1081.             Status=TRUE;                                    // Set The Status To TRUE
  1082.  
  1083.             glGenTextures(NUMBER_OF_TEXTURES, &Texture[0]);                    // Create The Texture
  1084.  
  1085.             for (loop1=0; loop1<NUMBER_OF_TEXTURES; loop1++)                    // Loop Through Textures
  1086.             {
  1087.                 // Create MipMapped Texture
  1088.                 glBindTexture(GL_TEXTURE_2D, Texture[loop1]);
  1089.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  1090.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  1091.                 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[loop1]->sizeX, TextureImage[loop1]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop1]->data);
  1092.             }//for
  1093.  
  1094.             for (loop1=0; loop1<NUMBER_OF_TEXTURES; loop1++)                    // Loop Through Textures
  1095.             {
  1096.                 if (TextureImage[loop1])                    // If Texture Exists    
  1097.                 {
  1098.                     if (TextureImage[loop1]->data)            // If Texture Image Exists
  1099.                         free(TextureImage[loop1]->data);    // Free The Texture Image Memory
  1100.  
  1101.                     free(TextureImage[loop1]);                // Free The Image Structure
  1102.                 }//if
  1103.             }//for
  1104.         }//if
  1105.  
  1106.     return Status;                                            // Return The Status
  1107.  
  1108. }// LoadGLTextures
  1109.  
  1110.  
  1111.  
  1112.  
  1113. //----------------------------------------------------------------------------------------
  1114. //        Description        :    Adjust Base Start Pos 
  1115. //----------------------------------------------------------------------------------------
  1116.  
  1117. void OpenGL :: SetTextBase(int Base)
  1118. {
  1119.     fStartPos = Base;
  1120.  
  1121. }// SetTextBase
  1122.  
  1123.  
  1124.  
  1125. //----------------------------------------------------------------------------------------
  1126. //        Description        :    Loads the font data into memory
  1127. //----------------------------------------------------------------------------------------
  1128.  
  1129. GLvoid OpenGL :: LoadFont()                    // Build Our Font Display List
  1130. {
  1131.     AUX_RGBImageRec *Texs[1];
  1132.  
  1133.     // Set Font Texture, How Many Characters per row and how many columns
  1134.     fxCount     = 16;
  1135.     fyCount     = 16;
  1136.  
  1137.     Texs[0] = NULL;
  1138.     Texs[0] = auxDIBImageLoad("data/font.bmp");                    // Load Font Texture
  1139.     
  1140.     if (!Texs[0])
  1141.         ErrorMsg("Texture not loaded");
  1142.     
  1143.     fTexture[0] = NULL;
  1144.     glGenTextures(1, &fTexture[0]);                                // Create Font Texture
  1145.     glBindTexture(GL_TEXTURE_2D, fTexture[0]);
  1146.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  1147.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  1148.     glTexImage2D(GL_TEXTURE_2D, 0, 3, Texs[0]->sizeX , Texs[0]->sizeY , 0, GL_RGB, GL_UNSIGNED_BYTE, Texs[0]->data);
  1149.  
  1150.     if(Texs[0]->data != NULL) free(Texs[0]->data);                // Free Data Allocated
  1151.     
  1152.  
  1153.     // Set Font Width and Font Height in Texture, and Character Spacing
  1154.     fWidth      = 16;
  1155.     fHeight     = 16;
  1156.     fSpacing    = 10;
  1157.  
  1158.     // Adjust Fonts 2D Drawing Rectangle Size 
  1159.     fdWidth  = 640;
  1160.     fdHeight = 480;
  1161.  
  1162.     int     loop;
  1163.     float    cx;                                            // Holds Our X Character Coord
  1164.     float    cy;                                            // Holds Our Y Character Coord
  1165.     float   cwx;                                        // CharWidth in texture units
  1166.     float   cwy;                                        // CharHeight in texture units
  1167.  
  1168.     cwx = (1.0f / 256.0f) * fWidth;
  1169.     cwy = (1.0f / 256.0f) * fHeight;
  1170.     
  1171.     fBase = glGenLists(fxCount * fyCount);                // Creating Display Lists
  1172.     glBindTexture(GL_TEXTURE_2D, fTexture[0]);            // Select Our Font Texture
  1173.     
  1174.     for (loop = 0; loop < (fxCount * fyCount); loop++)    // Loop Through All Lists
  1175.     {
  1176.         cx = float(loop % fxCount) * cwx;                // X Position Of Current Character
  1177.         cy = float(loop / fyCount) * cwy;                // Y Position Of Current Character
  1178.  
  1179.         glNewList(fBase + loop,GL_COMPILE);                // Start Building A List
  1180.             glBegin(GL_QUADS);                            // Use A Quad For Each Character
  1181.                 glTexCoord2f(cx, 1 - cy - cwy);            // Texture Coord (Bottom Left)
  1182.                 glVertex2i(0, 0);                        // Vertex Coord (Bottom Left)
  1183.                 glTexCoord2f(cx + cwx, 1 - cy - cwy);    // Texture Coord (Bottom Right)
  1184.                 glVertex2i(fWidth - 1,0);                // Vertex Coord (Bottom Right)
  1185.                 glTexCoord2f(cx + cwx, 1 - cy);            // Texture Coord (Top Right)
  1186.                 glVertex2i(fWidth - 1,fHeight - 1);        // Vertex Coord (Top Right)
  1187.                 glTexCoord2f(cx, 1 - cy);                // Texture Coord (Top Left)
  1188.                 glVertex2i(0, fHeight - 1);                // Vertex Coord (Top Left)
  1189.             glEnd();                                    // Done Building Our Quad (Character)
  1190.             glTranslated(fSpacing, 0, 0);                // Move To The Right Of The Character
  1191.         glEndList();                                    // Done Building The Display List
  1192.     }//for                                                    // Loop Until All Are Built
  1193. }// LoadFont
  1194.  
  1195.  
  1196.  
  1197.  
  1198. //----------------------------------------------------------------------------------------
  1199. //        Description        :    Displays text on screen at x,y position
  1200. //----------------------------------------------------------------------------------------
  1201.  
  1202. GLvoid OpenGL :: Print(GLint x, GLint y, const char *fmt, ...)    // Where The Printing Happens
  1203. {
  1204.  
  1205.     char        text[256];                                // Holds Our String
  1206.     va_list        ap;                                        // Pointer To List Of Arguments
  1207.  
  1208.     if (fmt == NULL)                                    // If There's No Text
  1209.         return;                                            // Do Nothing
  1210.  
  1211.     va_start(ap, fmt);                                    // Parses The String For Variables
  1212.     vsprintf(text, fmt, ap);                            // And Converts Symbols To Actual Numbers
  1213.     va_end(ap);                                            // Results Are Stored In Text
  1214.  
  1215.     glBindTexture(GL_TEXTURE_2D, fTexture[0]);            // Select Our Font Texture
  1216.     glEnable(GL_BLEND);                                    
  1217.     glDisable(GL_DEPTH_TEST);                            // Disables Depth Testing
  1218.     glDisable(GL_LIGHTING);
  1219.     glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  1220.     glPushMatrix();                                        // Store The Projection Matrix
  1221.     glLoadIdentity();                                    // Reset The Projection Matrix
  1222.     glOrtho(0,fdWidth,0,fdHeight,-1,1);                    // Set Up An Ortho Screen
  1223.     glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
  1224.     glPushMatrix();                                        // Store The Modelview Matrix
  1225.     glLoadIdentity();                                    // Reset The Modelview Matrix
  1226.     glTranslated(x,fdHeight - y,0);                        // Position The Text (0,0 - Bottom Left)
  1227.     glListBase(fBase - fStartPos);                        // Choose The Font Set (0 or 1)
  1228.     glCallLists(strlen(text),GL_BYTE,text);                // Write The Text To The Screen
  1229.     glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  1230.     glPopMatrix();                                        // Restore The Old Projection Matrix
  1231.     glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
  1232.     //Reset Old settings
  1233.       glPopMatrix();                                        // Restore The Old Projection Matrix
  1234.     glEnable(GL_LIGHTING);
  1235.     glEnable(GL_BLEND);
  1236.  
  1237. }// glPrint